package com.example.tictactoe;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ChessView chessView;
    private Button newGameButton;

    private AlertDialog.Builder alertDialog;

    private TextView countPlayer1Won;
    private TextView countPlayer2Won;
    private TextView countDraw;

    private Integer c1w = 0;
    private Integer c2w = 0;
    private Integer draw = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initDialog();
        Intent intent = getIntent();
        newGameButton = findViewById(R.id.new_game_button);
        chessView = findViewById(R.id.chess_view);
        countPlayer1Won = findViewById(R.id.countPlayer1Won);
        countPlayer2Won = findViewById(R.id.countPlayer2Won);
        countDraw = findViewById(R.id.countDraw);

        if (intent.getIntExtra("re",0) == 1){
            newGameButton.setVisibility(View.INVISIBLE);
            chessView.setOnTouchListener();
            draw = intent.getIntExtra("cntD", 0);
            c1w = intent.getIntExtra("cnt1", 0);
            c2w = intent.getIntExtra("cnt2", 0);
            countDraw.setText( "平局\n\n" + draw.toString());
            countPlayer1Won.setText("×方\n\n" + c1w.toString());
            countPlayer2Won.setText("○方\n\n" + c2w.toString());
        } else {
            newGameButton.setText("开始游戏");
            newGameButton.setOnClickListener(this);
        }


        chessView.setOnSuccessListener(new ChessView.OnSuccessListener() {
            @Override
            public void onSuccess(ChessView.Player player) {
                switch (player){
                    case NONE:
                        alertDialog.setMessage("平局！");
                        draw++;
                        break;
                    case USER_ONE:
                        alertDialog.setMessage("×方获胜");
                        c1w++;
                        break;
                    case USER_TWO:
                        alertDialog.setMessage("○方获胜！");
                        c2w++;
                        break;
                }
                alertDialog.show();
            }
        });
    }

    private void initDialog(){
        alertDialog = new AlertDialog.Builder(this);
        alertDialog.setTitle("游戏信息");
        alertDialog.setCancelable(false);
        alertDialog.setPositiveButton("再来一局", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(MainActivity.this, MainActivity.class);
                intent.putExtra("re", 1);
                intent.putExtra("cntD", draw.intValue());
                intent.putExtra("cnt1", c1w.intValue());
                intent.putExtra("cnt2", c2w.intValue());
                startActivity(intent);
                finish();
            }
        });
        alertDialog.setNegativeButton("返回", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                newGameButton.setVisibility(View.VISIBLE);
                newGameButton.setText("再来一局");
                countDraw.setText( "平局\n\n" + draw.toString());
                countPlayer1Won.setText("×方\n\n" + c1w.toString());
                countPlayer2Won.setText("○方\n\n" + c2w.toString());
                newGameButton.setOnClickListener(MainActivity.this);
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.new_game_button){
            if (newGameButton.getText().toString().equals("开始游戏")){
                Log.e("MainActivity:","开始游戏");
                chessView.setOnTouchListener();
            } else {
                Intent intent = new Intent(MainActivity.this, MainActivity.class);
                intent.putExtra("re", 1);
                intent.putExtra("cntD", draw.intValue());
                intent.putExtra("cnt1", c1w.intValue());
                intent.putExtra("cnt2", c2w.intValue());
                startActivity(intent);
                finish();
            }
        }
    }
}
